const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')


}

const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-');
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * number: 传入时间戳 
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致 
*/
function formatTimeTwo(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

//倒计时
function getTimeLeft(endTime, callback){
 // let endTime = 1564454889000;
  let newTime = new Date().getTime();
  let mss = endTime - newTime;
  //console.log('获取时间差',mss);
  if (mss < 0){
    console.log('已有倒计时结束');
    (callback && typeof (callback) === "function") && callback();
    return mss;
  }else{
    // 将时间差（毫秒）格式为：天时分秒
    let days = parseInt(mss / (1000 * 60 * 60 * 24));
    let hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    let minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    let seconds = parseInt((mss % (1000 * 60)) / 1000);
    //console.log(days + "天" + hours + "时" + minutes + "分" + seconds + "秒")
    days = days < 10 ? `0${days}` : days;
    //hours = days > 0 ? hours + days * 24 : hours;  
    hours = hours < 10 ? `0${hours}` : hours;
    minutes = minutes < 10 ? `0${minutes}` : minutes;
    seconds = seconds < 10 ? `0${seconds}` : seconds;
    let dataTime = {
      days: days,
      hours: hours,
      minutes: minutes,
      seconds: seconds
    }
    return dataTime;
  }
}


//百度经纬度转腾讯经纬度
function bMapTransTXmap(lng,lat){
  let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  let x = lng - 0.0065;
  let y = lat - 0.006;
  let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  let lngs = z * Math.cos(theta);
  let lats = z * Math.sin(theta);
  return {
    lng: lngs,
    lat: lats
  }
}

//腾讯经纬度转百度经纬度
function tXmapTransBMap(lng, lat){
  let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  let x = lng;
  let y = lat;
  let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  let lngs = z * Math.cos(theta) + 0.0065;
  let lats = z * Math.sin(theta) + 0.006;
  return {
    lng: lngs,
    lat: lats
  }
}

function timeCalculation(time) {
  var now = new Date().getTime();
  // 下面两种转换格式都可以。
  var tmpTime = time;
  // 返回值
  var result;
  // 一分钟 1000 毫秒 乘以 60
  var minute = 1000 * 60;
  var hour = minute * 60;
  var day = hour * 24;
  var week = day * 7;
  var month = day * 30;
  var year = day * 365;
  // 现在时间 减去 传入时间 得到差距时间
  var diffValue = now - tmpTime;
  // 小于 0 直接返回
  if (diffValue < 0) {
    return;
  }
  // 计算 差距时间除以 指定时间段的毫秒数
  var yearC = diffValue / year;
  var monthC = diffValue / month;
  var weekC = diffValue / week;
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;
  // 利用 布尔值 true 进入判断
  // 直接传数值进去做判断 结果不正确。
  switch (true) {
    case yearC >= 1:
      console.log("switch 年");
      // 如果大于一年就显示具体日期
      result = formatDate(tmpTime);
      break;
    case monthC >= 1:
      console.log("switch 月");
      result = "" + parseInt(monthC) + "月前";
      break;
    case weekC >= 1:
      console.log("switch 周");
      result = "" + parseInt(weekC) + "周前";
      break;
    case dayC >= 1:
      console.log("switch 日");
      result = "" + parseInt(dayC) + "天前";
      break;
    case hourC >= 1:
      console.log("switch 天");
      result = "" + parseInt(hourC) + "小时前";
      break;
    case minC >= 1:
      console.log("switch 分");
      result = "" + parseInt(minC) + "分钟前";
      break;
    default:
      result = "刚刚";
  }
  console.log(result)
  return result;
}

// 图片自适应
function imageUtil(e) {
  var imageSize = {};
  var originalWidth = e.detail.width;//图片原始宽
  var originalHeight = e.detail.height;//图片原始高
  var originalScale = originalHeight / originalWidth;
  //图片高宽比
  console.log('originalWidth: ' + originalWidth)
  console.log('originalHeight: ' + originalHeight)
  console.log('原始图片的高宽比:' + originalScale)

  //获取屏幕宽高
  wx.getSystemInfo({
    success: function (res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      var windowscale = windowHeight / windowWidth;
      //屏幕高宽比
      console.log('windowWidth: ' + windowWidth)
      console.log('windowHeight: ' + windowHeight)
      if (originalScale < windowscale && originalWidth >= windowWidth*0.8) {
        //图片高宽比小于屏幕高宽比
        //图片缩放后的宽为屏幕宽
        console.log('1')
        imageSize.imageWidth  = windowWidth * 0.8;
        imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth * 0.8;
      } 
    else  if (originalScale < windowscale && originalWidth < windowWidth*0.8){
        console.log('2')
        imageSize.imageWidth  = originalWidth;
        imageSize.imageHeight = originalHeight;
      }
      else if (originalScale > windowscale && originalHeight >= windowHeight * 0.8){
        //图片高宽比大于屏幕高宽比
        //图片缩放后的高为屏幕高
        console.log('3')
        imageSize.imageHeight = windowHeight * 0.8;
        imageSize.imageWidth  = (windowHeight * originalWidth) / originalHeight * 0.8;
      }
      else if (originalScale > windowscale && originalHeight < windowHeight * 0.8) {
        console.log('4')
        imageSize.imageWidth = originalWidth;
        imageSize.imageHeight = originalHeight;
      }
    
    }
  })
  var originalScale2 = imageSize.imageHeight / imageSize.imageWidth;
  console.log('缩放后的宽: ' + imageSize.imageWidth)
  console.log('缩放后的高: ' + imageSize.imageHeight)
  console.log('压缩后图片的高宽比:' + originalScale2)
  return imageSize;
}
 


module.exports = {
  formatTime: formatTime,
  formatTimeTwo: formatTimeTwo,
  getTimeLeft: getTimeLeft,
  bMapTransTXmap: bMapTransTXmap,
  tXmapTransBMap: tXmapTransBMap,
  formatDate:formatDate,
  timeCalculation: timeCalculation,
  imageUtil: imageUtil
}
